local ExcelNpcConf = require("excel.Npc")
local ExcelMonsterConf = require("excel.Monster")
local ExcelCollectConf = require("excel.Collect")
local ExcelCopyAimConf = require("excel.CopyAim")
local SceneEvent = require("core.SceneEvent")
local Msg = require("core.Msg")
local CopyDefine = require("copy.Define")
local Copy = require("copy.CopyL")
local Aim = require("copy.Aim")
local TaskHelp = require("task.TaskHelp")

local DescCache = {} -- 目标描述缓存 因为目标描述里面有些特殊字符是要替换的 为了避免频繁每次都字符串查找替换浪费cpu

----------------------------
-- 打怪目标相关逻辑 begin --
function monster_MonsterDie(killer, monster, monsterID, aimID, aimConfig, copyID, roomID)
	if Aim.aimRoomStatus[roomID] == nil then return end
	if Aim.aimRoomStatus[roomID][aimID] == nil then return end
	if Aim.aimRoomStatus[roomID][aimID].status ~= CopyDefine.AIM_STATUS_DOING_CANNOT_FINISH then return end

	local aimRecord = Aim.aimRoomStatus[roomID][aimID]
	local allFinish = true
	local bChanged = false
	for targetID, targetNum in pairs(aimConfig.targetParam) do
		if targetID == monsterID then
			if aimRecord[targetID] == nil then
				aimRecord[targetID] = 1
				bChanged = true
			elseif aimRecord[targetID] >= targetNum then
			else
				aimRecord[targetID] = aimRecord[targetID] + 1
				bChanged = true
			end
		end	
		
		if aimRecord[targetID] == nil or aimRecord[targetID] < targetNum then
			allFinish = false
		end
	end
	
	if bChanged == false then return end
	
	Aim.sendAimStatus(copyID, roomID, aimID)
	if allFinish == true then 
		aimRecord.status = CopyDefine.AIM_STATUS_DOING_CAN_FINISH 
		
		playMovie(copyID, roomID, aimConfig.movie2)
		raiseSceneEvent(copyID, roomID, aimConfig.event2)
		
		if aimConfig.endNpcId == 0 then			-- 自动完成
			if Aim.aimCanFinish(aimID, copyID, roomID, nil) == CopyDefine.COPY_AIM_FINISH_OK then
				Aim.aimDoFinish(aimID, copyID, roomID)
			end
			return
		else
			Aim.sendAimStatus(copyID, roomID, aimID)
		end
	end
end
-- 打怪目标相关逻辑 end
----------------------------

----------------------------
-- 打怪目标(特定id团)相关逻辑 begin --
function monsterTable_MonsterDie(killer, monster, monsterID, aimID, aimConfig, copyID, roomID)
	if Aim.aimRoomStatus[roomID] == nil then return end
	if Aim.aimRoomStatus[roomID][aimID] == nil then return end
	if Aim.aimRoomStatus[roomID][aimID].status ~= CopyDefine.AIM_STATUS_DOING_CANNOT_FINISH then return end

	local aimRecord = Aim.aimRoomStatus[roomID][aimID]
	local bChanged = false
	
	for i = 1, #aimConfig.targetParam.target do
		if aimConfig.targetParam.target[i] == monsterID then
			if aimRecord[0] == nil then
				aimRecord[0] = 1
				bChanged = true
			elseif aimRecord[0] >= aimConfig.targetParam.count then
			else 
				aimRecord[0] = aimRecord[0] + 1
				bChanged = true
			end
			break
		end
	end
	if bChanged == false then return end
	
	Aim.sendAimStatus(copyID, roomID, aimID)
	
	if aimRecord[0] == aimConfig.targetParam.count then
		aimRecord.status = CopyDefine.AIM_STATUS_DOING_CAN_FINISH 
		
		playMovie(copyID, roomID, aimConfig.movie2)
		raiseSceneEvent(copyID, roomID, aimConfig.event2)
		
		if aimConfig.endNpcId == 0 then			-- 自动完成
			if Aim.aimCanFinish(aimID, copyID, roomID, nil) == CopyDefine.COPY_AIM_FINISH_OK then
				Aim.aimDoFinish(aimID, copyID, roomID)
			end
			return
		else
			Aim.sendAimStatus(copyID, roomID, aimID)
		end
	end
end
-- 打怪目标(特定id团)相关逻辑 end
----------------------------

----------------------------
-- 采集目标相关逻辑 begin --
function collect_CollectDie(killer, collect, collectID, aimID, aimConfig, copyID, roomID)
	if Aim.aimRoomStatus[roomID] == nil then return end
	if Aim.aimRoomStatus[roomID][aimID] == nil then return end
	if Aim.aimRoomStatus[roomID][aimID].status ~= CopyDefine.AIM_STATUS_DOING_CANNOT_FINISH then return end
	
	local aimRecord = Aim.aimRoomStatus[roomID][aimID]
	local allFinish = true
	local bChanged = false
	for targetID, targetNum in pairs(aimConfig.targetParam) do
		if targetID == collectID then
			if aimRecord[targetID] == nil then
				aimRecord[targetID] = 1
				bChanged = true
			elseif aimRecord[targetID] >= targetNum then
			else 
				aimRecord[targetID] = aimRecord[targetID] + 1
				bChanged = true
			end
		end	
		
		if aimRecord[targetID] == nil or aimRecord[targetID] < targetNum then
			allFinish = false
		end
	end
	
	if bChanged == false then return end
	
	Aim.sendAimStatus(copyID, roomID, aimID)	
	if allFinish == true then 
		aimRecord.status = CopyDefine.AIM_STATUS_DOING_CAN_FINISH 
		
		playMovie(copyID, roomID, aimConfig.movie2)
		raiseSceneEvent(copyID, roomID, aimConfig.event2)
		
		if aimConfig.endNpcId == 0 then			-- 自动完成
			if Aim.aimCanFinish(aimID, copyID, roomID, nil) == CopyDefine.COPY_AIM_FINISH_OK then
				Aim.aimDoFinish(aimID, copyID, roomID)
			end
			return
		else
			Aim.sendAimStatus(copyID, roomID, aimID)
		end
	end
end
-- 采集目标相关逻辑 end

-- 探索区域
function area_reach(aimID, aimConfig, copyID, roomID)
  if Aim.aimRoomStatus[roomID] == nil then return end
  if Aim.aimRoomStatus[roomID][aimID] == nil then return end
  if Aim.aimRoomStatus[roomID][aimID].status ~= CopyDefine.AIM_STATUS_DOING_CANNOT_FINISH then return end

  local aimRecord = Aim.aimRoomStatus[roomID][aimID]

  aimRecord.status = CopyDefine.AIM_STATUS_DOING_CAN_FINISH 
    
  playMovie(copyID, roomID, aimConfig.movie2)
  raiseSceneEvent(copyID, roomID, aimConfig.event2)
    
  if aimConfig.endNpcId == 0 then     -- 自动完成
     if Aim.aimCanFinish(aimID, copyID, roomID, nil) == CopyDefine.COPY_AIM_FINISH_OK then
        Aim.aimDoFinish(aimID, copyID, roomID)
     end
     return
  else
      Aim.sendAimStatus(copyID, roomID, aimID)
  end
end
----------------------------

aimActionFuncList = aimActionFuncList or {} 
aimActionFuncList[CopyDefine.COPY_AIM_ACTION_TALK] = {}												-- 对话目标
aimActionFuncList[CopyDefine.COPY_AIM_ACTION_MONSTER] = {monsterDie = monster_MonsterDie}			-- 打怪目标(特定id)
aimActionFuncList[CopyDefine.COPY_AIM_ACTION_COLLECT] = {collectDie = collect_CollectDie}			-- 采集目标
aimActionFuncList[CopyDefine.COPY_AIM_ACTION_MONSTER_TABLE] = {monsterDie = monsterTable_MonsterDie}-- 打怪目标(特定id团)
aimActionFuncList[CopyDefine.COPY_AIM_ACTION_BLANK] = {}											-- 空白（由地图事件完成）
aimActionFuncList[CopyDefine.COPY_AIM_ACTION_BLANK_N] = {}											-- 空白and地图n（由地图事件完成）
aimActionFuncList[CopyDefine.COPY_AIM_ACTION_REACH_AREA] = {reachArea = area_reach} -- 到达区域

function getStatusInfoByAction(copyID, roomID, aimID, actionObjs)
	local retStatus
	local retDesc
	local retTime = 0
	local aimConfig = ExcelCopyAimConf[copyID][aimID]
	local aimRecord = Aim.aimRoomStatus[roomID]
	
	if aimRecord[aimID] == nil then
		retStatus = CopyDefine.AIM_STATUS_NOT_ACCEPT
		retDesc = replaceDesc(aimConfig.desc1, copyID, aimConfig, aimID, 1)
		actionObjs[0] = 0
	elseif aimRecord[aimID].status == CopyDefine.AIM_STATUS_FINISHED then
		retStatus = aimRecord[aimID].status
		retDesc = ""
		actionObjs[0] = 0
	elseif aimRecord[aimID].status == CopyDefine.AIM_STATUS_DOING_CANNOT_FINISH then
		retStatus = aimRecord[aimID].status
		if aimConfig.targetTime > 0 then
			retTime = aimConfig.targetTime - os.time() + aimRecord[aimID].beginTime
		end
		
		retDesc = replaceDesc(aimConfig.desc2, copyID, aimConfig, aimID, 2)
		
		actionObjs[0] = 0
		if aimConfig.target == CopyDefine.COPY_AIM_ACTION_MONSTER then
			local count = 0
			for targetID, targetNum in pairs(aimConfig.targetParam) do
				count = count + 1
				actionObjs[count].id = targetID
				if aimRecord[aimID] ~= nil and aimRecord[aimID][targetID] ~= nil then
					actionObjs[count].cur = aimRecord[aimID][targetID]
				else
					actionObjs[count].cur = 0				
				end
				actionObjs[count].max = targetNum
				actionObjs[count].map = ExcelMonsterConf.monster[targetID].mapID
				actionObjs[count].x = ExcelMonsterConf.monster[targetID].mapX
				actionObjs[count].y = ExcelMonsterConf.monster[targetID].mapY
			end
			actionObjs[0] = count
		elseif aimConfig.target == CopyDefine.COPY_AIM_ACTION_MONSTER_TABLE then
			actionObjs[1].id = 0
			if aimRecord[aimID] ~= nil and aimRecord[aimID][0] ~= nil then
				actionObjs[1].cur = aimRecord[aimID][0]
			else
				actionObjs[1].cur = 0
			end
			actionObjs[1].max = aimConfig.targetParam.count
			actionObjs[1].map = 0
			actionObjs[1].x = 0
			actionObjs[1].y = 0	
			actionObjs[0] = 1
		elseif aimConfig.target == CopyDefine.COPY_AIM_ACTION_COLLECT then
			local count = 0
			for targetID, targetNum in pairs(aimConfig.targetParam) do
				count = count + 1
				actionObjs[count].id = targetID
				if aimRecord[aimID] ~= nil and aimRecord[aimID][targetID] ~= nil then
					actionObjs[count].cur = aimRecord[aimID][targetID]
				else
					actionObjs[count].cur = 0				
				end
				actionObjs[count].max = targetNum
				actionObjs[count].map = ExcelCollectConf.Collect[targetID].mapID
				actionObjs[count].x = ExcelCollectConf.Collect[targetID].mapX
				actionObjs[count].y = ExcelCollectConf.Collect[targetID].mapY
			end
			actionObjs[0] = count
		elseif aimConfig.target == CopyDefine.COPY_AIM_ACTION_BLANK_N then
			actionObjs[1].id = 0
			actionObjs[1].cur = 0
			local mapList = Copy.copyList[copyID][roomID]
			for mapID, sceneID in pairs(mapList) do
				if mapID == aimConfig.mapID then
					if SceneEvent.n[sceneID] then
						actionObjs[1].cur = SceneEvent.n[sceneID]
					end
					break	
				end
			end
			actionObjs[1].max = aimConfig.targetParam[1]
			actionObjs[1].map = 0
			actionObjs[1].x = 0
			actionObjs[1].y = 0
			actionObjs[0] = 1
		elseif aimConfig.target == CopyDefine.COPY_AIM_ACTION_REACH_AREA then
		  actionObjs[1].id  = 0
      actionObjs[1].cur = 0
      actionObjs[1].max = 1
      actionObjs[1].map = aimConfig.targetParam[1]
      actionObjs[1].x = aimConfig.targetParam[2]
      actionObjs[1].y = aimConfig.targetParam[3]
      actionObjs[0] = 1
		end
	elseif aimRecord[aimID].status == CopyDefine.AIM_STATUS_FAIL then
		retStatus = aimRecord[aimID].status
		retDesc = replaceDesc(aimConfig.desc2, copyID, aimConfig, aimID, 2)
		actionObjs[0] = 0  
	else
		retStatus = aimRecord[aimID].status
		if aimConfig.targetTime > 0 then
			retTime = aimConfig.targetTime - os.time() + aimRecord[aimID].beginTime
		end
		retDesc = replaceDesc(aimConfig.desc3, copyID, aimConfig, aimID, 3)
		actionObjs[0] = 0
	end
	
	return retStatus, retDesc, retTime
end

function playMovie(copyID, roomID, movieID)
	if movieID == nil or movieID[1] == nil then return end
	-- 播放动画		
	local mm = Msg.gc.GC_SCRIPT_START
	mm.script_id = movieID[1]
	mm.delay = movieID[2]
	Copy.sendCopy(mm, copyID, roomID)
end

function raiseSceneEvent(copyID, roomID, eventTable)
	if eventTable == nil or #eventTable ~= 2 then return end

	-- 触发地图事件
	local tempMapID = eventTable[1]
	local mapList = Copy.copyList[copyID][roomID]
	for mapID,sceneID in pairs(mapList)	do
		if mapID == tempMapID then
			SceneEvent.doSceneEvent(sceneID, eventTable[2])
			break
		end
	end	
end


--[[
<beginnpc> 代表接任务npc信息
<endnpc> 代表交任务npc信息
<monster:id> 代表打怪对象id信息
<collect:id> 代表采集对象id信息
<item:id>	代表收集对象id信息
<targetcnt:id> 目标数量 -- 这个起始服务器端不处理 直接交给客户端处理
<place:101:50:100:> 特定地点
<target:101:50:100:10001:1:> 特定目标
<npc:101> 特定npc信息
<sa:1:>特定操作 1采矿 2钓鱼
<open:1:>打开面板 具体定义见客户端的OpenPanel.as(亲！！看神马客户端，还是以服务端的PanelDefine.lua为准)
寻路信息里面 1 npc 2 采集物 3 怪物
--]]
function replaceDesc(desc, copyID, aimConfig, aimID, descIndex)
	if desc == nil or #desc == 0 then return "" end

	if DescCache[copyID] == nil then DescCache[copyID] = {} end
	if DescCache[copyID][aimID] == nil then DescCache[copyID][aimID] = {} end
	if DescCache[copyID][aimID][descIndex] ~= nil then return DescCache[copyID][aimID][descIndex] end

	local temp = desc
	
	local i, j
	while (true) do
		i, j = string.find(temp, "<endnpc>")
		if not i then break end
		
		local tempLeft = string.sub(temp, 1, i - 1)
		local tempRight = string.sub(temp, j + 1)
		temp = tempLeft .. "<a href='event:" .. ExcelNpcConf.npc[aimConfig.endNpcId].mapID .. "," .. ExcelNpcConf.npc[aimConfig.endNpcId].mapX .. "," .. ExcelNpcConf.npc[aimConfig.endNpcId].mapY .. "," .. aimConfig.endNpcId .. ",1".. "'><u>"  .. "<font color='#00ff00'>" .. ExcelNpcConf.npc[aimConfig.endNpcId].name .. "</font>" .. "</u></a>" .. tempRight 
	end

	i, j = string.find(temp, "<beginnpc>")
	if i then
		local tempLeft = string.sub(temp, 1, i - 1)
		local tempRight = string.sub(temp, j + 1)
		temp = tempLeft .. "<a href='event:" .. ExcelNpcConf.npc[aimConfig.startNpcId].mapID .. "," .. ExcelNpcConf.npc[aimConfig.startNpcId].mapX .. "," .. ExcelNpcConf.npc[aimConfig.startNpcId].mapY .. "," .. aimConfig.startNpcId .. ",1".. "'><u>"  .. "<font color='#00ff00'>" .. ExcelNpcConf.npc[aimConfig.startNpcId].name .. "</font>" .. "</u></a>" .. tempRight 
	end
	
	temp = TaskHelp.handleMonster(temp)
	
	temp = TaskHelp.handleCollect(temp)
	
	temp = TaskHelp.handleItem(temp)
	
	temp = TaskHelp.handleNpc(temp)
	
	temp = TaskHelp.handlePlace(temp)

	temp = TaskHelp.handleTarget(temp)

	temp = TaskHelp.handleSA(temp)
	
	temp = TaskHelp.handleOpen(temp)
	
	DescCache[copyID][aimID][descIndex] = temp
	return temp
end